/*******************************************************************************
* Project: QSAR(Cute Software Architecture) 
*
* Copyright: Copyright(C) 2024 by YuetingBen, All Rights Reserved
*
* File name: Ea.c
*
* Brief: ;
*
* Author: Yueting.Ben
*
* Module: Ea
*
*******************************************************************************/

/** HEADER FILES **/
#include "Ea.h"


/** MACRO DEFINITION **/


/** TYPEDEF DEFINITION **/


/** LOCAL DATA DECLARATION **/


/** GLOBAL DATA DECLARATION **/


/** LOCAL FUNCTION DECLARATION **/
static Std_ReturnType Ea_BlockIndexLookUp(
    uint16 BlockNumber,
    uint8 *index
);

/** GLOBAL FUNCTION DECLARATION **/
void Ea_GetVersionInfo(
    Std_VersionInfoType* versionInfo
);

Std_ReturnType Ea_Read(
    uint16 BlockNumber,
    uint16 BlockOffset, 
    uint8* DataBufferPtr, 
    uint16 Length
);

Std_ReturnType Ea_Write(
    uint16 BlockNumber, 
    const uint8* DataBufferPtr
);

/** LOCAL DATA **/


/** GLOBAL DATA **/


/** LOCAL FUNCTION **/

/*******************************************************************************
* -Name            Ea_BlockIndexLookUp
* -Brief           Lookup index in table EaBlockConfiguration refer to BlockNumber
* -Details         None
* -Param[in]       BlockNumber: Number of logical block, also denoting start address of that block in EEPROM
* -Param[out]      index: Pointer to the version information of this module
* -Return          None          
*******************************************************************************/
static Std_ReturnType Ea_BlockIndexLookUp(
    uint16 BlockNumber,
    uint8 *index
)
{
    Std_ReturnType retVal = E_NOT_OK;
    uint8 i;

    for(i = 0; i < EA_BLOCK_NUM; i++)
    {
        if(BlockNumber == EaBlockConfiguration[i].EaBlockNumber)
        {
            *index = i;
            retVal = E_OK;
            break;
        }
    }

    return(retVal);
}

/** GLOBAL FUNCTION **/

/*******************************************************************************
* -Name            Ea_GetVersionInfo
* -Brief           Returns the version information of this module 
* -Details         None
* -Param[out]      versioninfo: Pointer to the version information of this module
* -Return          None          
*******************************************************************************/
void Ea_GetVersionInfo(
    Std_VersionInfoType* versionInfo
)
{
    versionInfo->vendorID = (uint16)YSAR_VENDOR_ID;
    versionInfo->moduleID = (uint8)EA_MODULE_ID;
    versionInfo->sw_major_version = (uint8)EA_SW_MAJOR_VERSION;
    versionInfo->sw_minor_version = (uint8)EA_SW_MINOR_VERSION;
    versionInfo->sw_patch_version = (uint8)EA_SW_PATCH_VERSION;
}


/*******************************************************************************
* -Name            Ea_Read
* -Brief           Reads Length bytes of block Blocknumber at offset BlockOffset into the buffer DataBufferPtr
* -Details         None
* -Param[in]       BlockNumber: Number of logical block, also denoting start address of that block in EEPROM
* -Param[in]       BlockOffset: Read address offset inside the block
* -Param[in]       Length: Number of bytes to read
* -Param[out]      DataBufferPtr: Pointer to data buffer
* -Return          E_OK: The requested job has been accepted by the module
                   E_NOT_OK: The requested job has not been accepted by the EA module
*******************************************************************************/
Std_ReturnType Ea_Read(
    uint16 BlockNumber,
    uint16 BlockOffset, 
    uint8* DataBufferPtr, 
    uint16 Length
)
{
    Std_ReturnType retVal = E_NOT_OK;
    uint8 index;

    retVal = Ea_BlockIndexLookUp(BlockNumber, (uint8*)&index);
    
    if(E_OK == retVal)
    {
        MemAcc_Read(EaBlockConfiguration[index].EaMemAccAddressressArea->MemAccAddressAreaId,
                     (BlockNumber + BlockOffset),
                     DataBufferPtr,
                     Length);
    }
    
    return(retVal);
}


/*******************************************************************************
* -Name            Ea_Write
* -Brief           Reads Length bytes of block Blocknumber at offset BlockOffset into the buffer DataBufferPtr
* -Details         None
* -Param[in]       BlockNumber: Number of logical block, also denoting start address of that block in EEPROM
* -Param[in]       DataBufferPtr: Pointer to data buffer
* -Return          E_OK: The requested job has been accepted by the module
                   E_NOT_OK: The requested job has not been accepted by the EA module       
*******************************************************************************/
Std_ReturnType Ea_Write(
    uint16 BlockNumber, 
    const uint8* DataBufferPtr
)
{
    Std_ReturnType retVal = E_NOT_OK;
    uint8 index;

    retVal = Ea_BlockIndexLookUp(BlockNumber, (uint8*)&index);
    
    if(E_OK == retVal)
    {
        MemAcc_Write(EaBlockConfiguration[index].EaMemAccAddressressArea->MemAccAddressAreaId,
                     EaBlockConfiguration[index].EaBlockNumber,
                     DataBufferPtr,
                     EaBlockConfiguration[index].EaBlockSize);
    }
    
    return(retVal);
}

/*******************************************************************************
* -Name            Ea_MainFunction
* -Brief           Service to handle the requested jobs and the internal management operations
* -Details         None
* -Param[in]       None
* -Return          None   
*******************************************************************************/
void Ea_MainFunction(
    void
)
{

}


